import 'package:flutter/material.dart';

class ValueListenableBuilderPage extends StatefulWidget {
  @override
  _ValueListenableBuilderPageState createState() =>
      _ValueListenableBuilderPageState();
}

class _ValueListenableBuilderPageState
    extends State<ValueListenableBuilderPage> {
  final ValueNotifier<int> _counter = ValueNotifier<int>(0);

  @override
  Widget build(BuildContext context) {
    print('Main build called');
    return Scaffold(
      appBar: AppBar(title: Text('ValueListenableBuilder Demo')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('按按钮增加计数，只有ValueListenableBuilder会重建:'),
            ValueListenableBuilder<int>(
              valueListenable: _counter,
              builder: (context, value, child) {
                print('ValueListenableBuilder build called');
                return Text(
                  '点击了 $value 次',
                  style: Theme.of(context).textTheme.titleLarge,
                );
              },
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => _counter.value++,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
